package com.solcoo.merchant.ui.exp;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.android.common.util.DialogUtil;
import com.android.common.util.ToastUtil;
import com.solcoo.merchant.ApplicationExt;
import com.solcoo.merchant.Constant;
import com.solcoo.merchant.R;
import com.solcoo.merchant.base.BaseActivity;
import com.solcoo.merchant.base.task.TaskHelper;
import com.solcoo.merchant.base.task.TaskHelper.Callback;
import com.solcoo.merchant.base.task.TaskHelper.DefaultCallback;
import com.solcoo.merchant.model.PayResult;
import com.solcoo.merchant.ui.pay.PaySuccessActivity;
import com.solcoo.merchant.util.CheckUtil;
import com.solcoo.merchant.util.CheckUtil.CheckResult;
import com.solcoo.merchant.util.ViewUtil;

public class ExpActivity extends BaseActivity {
	String userTel;

	EditText expInput;

	Dialog mDialog;

	EditText cardNoInput;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.exp);
		expInput = (EditText) findViewById(R.id.exp);
		cardNoInput = (EditText) findViewById(R.id.card_no);
		mDialog = DialogUtil.createDialog(this, null, null, null, "增加经验值成功",
				null);
	}

	private Callback<PayResult> callback = new DefaultCallback<PayResult>(this) {

		@Override
		public void onSuccess(PayResult result) {
			Intent it = new Intent(ExpActivity.this, PaySuccessActivity.class);
			it.putExtra(PaySuccessActivity.INTENT_KEK_TITLE, "经验修改结果");
			it.putExtra(PaySuccessActivity.INTENT_KEK_INFO, result.msg);
			it.putExtra(PaySuccessActivity.INTENT_KEK_MSG, "增加经验成功");
			startActivity(it);
			finish();
		}

		public void onFail(PayResult result) {
			Intent it = new Intent(ExpActivity.this, PaySuccessActivity.class);
			it.putExtra(PaySuccessActivity.INTENT_KEK_TITLE, "经验修改结果");
			it.putExtra(PaySuccessActivity.INTENT_KEK_INFO, result.msg);
			it.putExtra(PaySuccessActivity.INTENT_KEK_FAIL, result.failMsg);
			startActivity(it);
			finish();
		}
	};

	public void sendOnClick(View v) {
		String exp = getExp();
		CheckResult checkMoney = CheckUtil.checkExp(exp);
		if (!checkMoney.isSuccess()) {
			ToastUtil.showToast(this, checkMoney.failMsg);
			return;
		}

		String no = cardNoInput.getText().toString();

		CheckResult checkNo = CheckUtil.checkCardNoOrPhoneNo(no);
		if (!checkNo.isSuccess()) {
			ToastUtil.showToast(this, checkNo.failMsg);
			return;
		}

		TaskHelper.oper(this,
				Constant.URL.getExp(ApplicationExt.getUser().yonghum, no, exp),
				callback, PayResult.class);
	}

	public void qrcodeOnClick(View v) {
		ViewUtil.startCapture(v.getContext(), 0);
	}

	public void rechargeOnClick(View v) {
		String money = getExp();

		CheckResult checkResult = CheckUtil.checkMoney(money);
		if (checkResult.isSuccess()) {
			TaskHelper.oper(this, Constant.URL.getRecharge(
					ApplicationExt.getUser().yonghum, userTel, money),
					callback, PayResult.class);
		} else {
			ToastUtil.showToast(this, checkResult.failMsg);
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK) {
			Bundle bundle = data.getExtras();
			String scanResult = bundle.getString("result");
			CheckResult checkResult = CheckUtil
					.checkCardNoOrPhoneNo(scanResult);
			if (checkResult.isSuccess()) {
				cardNoInput.setText(scanResult);
			} else {
				ToastUtil.showToast(this, "扫描内容为：" + scanResult);
			}
		}
	}

	private String getExp() {
		return expInput.getText().toString();
	}
}
